#!/usr/bin/env python3
"""
清除Python缓存文件

删除所有__pycache__目录和.pyc文件
"""

import os
import shutil
import sys

def clear_pycache(root_dir):
    """清除指定目录下的所有Python缓存"""
    count_dirs = 0
    count_files = 0
    
    print(f"扫描目录: {root_dir}")
    print("-" * 60)
    
    for dirpath, dirnames, filenames in os.walk(root_dir):
        # 删除__pycache__目录
        if '__pycache__' in dirnames:
            pycache_path = os.path.join(dirpath, '__pycache__')
            try:
                shutil.rmtree(pycache_path)
                print(f"已删除: {pycache_path}")
                count_dirs += 1
            except Exception as e:
                print(f"无法删除 {pycache_path}: {e}")
        
        # 删除.pyc文件
        for filename in filenames:
            if filename.endswith('.pyc') or filename.endswith('.pyo'):
                file_path = os.path.join(dirpath, filename)
                try:
                    os.remove(file_path)
                    print(f"已删除: {file_path}")
                    count_files += 1
                except Exception as e:
                    print(f"无法删除 {file_path}: {e}")
    
    print("-" * 60)
    print(f"清除完成!")
    print(f"  删除 __pycache__ 目录: {count_dirs} 个")
    print(f"  删除 .pyc/.pyo 文件: {count_files} 个")

if __name__ == "__main__":
    # 获取项目根目录
    project_root = os.path.dirname(os.path.abspath(__file__))
    
    print("=" * 60)
    print("清除Python缓存")
    print("=" * 60)
    print()
    
    # 清除缓存
    clear_pycache(project_root)
    
    print("\n提示: 现在可以重新运行您的程序了")
    print("例如: python examples/simple_demo.py")
